/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <ktest/ktest.h>

#include <kmhdfs/common/exception.h>
#include <kmhdfs/common/xml_config.h>

#ifndef DATA_DIR
#define DATA_DIR ""
#endif

using namespace Hdfs;

TEST(TestXmlConfig, TestParse) {
    EXPECT_NO_THROW({ Config conf(DATA_DIR"hdfs-default.xml"); });
}

TEST(TestXmlConfig, ParseBadFile) {
    EXPECT_THROW({ Config conf("path not exist"); }, HdfsBadConfigFoumat);
    EXPECT_THROW({ Config conf(DATA_DIR"invalid.xml"); }, HdfsBadConfigFoumat);
}

TEST(TestXmlConfig, TestGetValue) {
    Config conf(DATA_DIR"unit-config.xml");
    EXPECT_STREQ("TestString", conf.getString("TestString"));
    EXPECT_STREQ("TestString", conf.getString("NOT EXIST", "TestString"));
    EXPECT_EQ(123456, conf.getInt32("TestInt32"));
    EXPECT_EQ(123456, conf.getInt32("NOT EXIST", 123456));
    EXPECT_THROW(conf.getInt32("TestInt32Invalid"), HdfsConfigNotFound);
    EXPECT_THROW(conf.getInt32("TestInt32OverFlow"), HdfsConfigNotFound);
    EXPECT_EQ(12345678901ll, conf.getInt64("TestInt64"));
    EXPECT_EQ(12345678901ll, conf.getInt64("NOT EXIST", 12345678901ll));
    EXPECT_THROW(conf.getInt64("TestInt64Invalid"), HdfsConfigNotFound);
    EXPECT_THROW(conf.getInt64("TestInt64OverFlow"), HdfsConfigNotFound);
    EXPECT_DOUBLE_EQ(123.456, conf.getDouble("TestDouble"));
    EXPECT_DOUBLE_EQ(123.456, conf.getDouble("NOT EXIST", 123.456));
    EXPECT_THROW(conf.getDouble("TestDoubleInvalid"), HdfsConfigNotFound);
    EXPECT_THROW(conf.getDouble("TestDoubleOverflow"), HdfsConfigNotFound);
    EXPECT_THROW(conf.getDouble("TestDoubleUnderflow"), HdfsConfigNotFound);
    EXPECT_TRUE(conf.getBool("TestTrue1"));
    EXPECT_TRUE(conf.getBool("TestTrue2"));
    EXPECT_TRUE(conf.getBool("NOT EXIST", true));
    EXPECT_FALSE(conf.getBool("TestFalse1"));
    EXPECT_FALSE(conf.getBool("TestFalse2"));
    EXPECT_FALSE(conf.getBool("NOT EXIST", false));
    EXPECT_THROW(conf.getBool("TestBoolInvalid"), HdfsConfigNotFound);
}
